package com.example.project.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.project.core.text.Convert;
import com.example.project.entity.Issue;
import com.example.project.entity.TimeEntrie;
import com.example.project.mapper.TimeEntrieMapper;
import com.example.project.service.TimeEntrieService;
import com.example.project.utils.DateUtils;

/**
 * 时间记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-17
 */
@Service
public class TimeEntrieServiceImpl extends BaseServiceImpl implements TimeEntrieService {

	@Autowired
	private TimeEntrieMapper timeEntriesMapper;

	/**
	 * 查询时间记录
	 * 
	 * @param id 时间记录主键
	 * @return 时间记录
	 */
	@Override
	public TimeEntrie selectTimeEntriesById(Long id) {
		return timeEntriesMapper.selectTimeEntriesById(id);
	}

	/**
	 * 查询时间记录列表
	 * 
	 * @param timeEntries 时间记录
	 * @return 时间记录
	 */
	@Override
	public List<TimeEntrie> selectTimeEntriesList(TimeEntrie timeEntries) {
		return timeEntriesMapper.selectTimeEntriesList(timeEntries);
	}

	/**
	 * 新增时间记录
	 * 
	 * @param timeEntries 时间记录
	 * @return 结果
	 */
	@Override
	public int insertTimeEntries(TimeEntrie timeEntries) {
		timeEntries.setCreateTime(DateUtils.curLocalDateTime());
		return timeEntriesMapper.insertTimeEntries(timeEntries);
	}

	/**
	 * 修改时间记录
	 * 
	 * @param timeEntries 时间记录
	 * @return 结果
	 */
	@Override
	public int updateTimeEntries(TimeEntrie timeEntries) {
		timeEntries.setUpdateTime(DateUtils.curLocalDateTime());
		return timeEntriesMapper.updateTimeEntries(timeEntries);
	}

	/**
	 * 批量删除时间记录
	 * 
	 * @param ids 需要删除的时间记录主键
	 * @return 结果
	 */
	@Override
	public int deleteTimeEntriesByIds(String ids) {
		return timeEntriesMapper.deleteTimeEntriesByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除时间记录信息
	 * 
	 * @param id 时间记录主键
	 * @return 结果
	 */
	@Override
	public int deleteTimeEntriesById(Long id) {
		return timeEntriesMapper.deleteTimeEntriesById(id);
	}

	@Override
	public int save(TimeEntrie entrie, Issue issue, Long userId) {
		// TODO Auto-generated method stub
		entrie.setProjectId(issue.getProjectId());
		entrie.setUserId(userId);
		entrie.setAuthorId(userId);
		LocalDate now = LocalDate.now();
		entrie.setSpentOn(now);
		entrie.setTyear(now.getYear());
		entrie.setTmonth(now.getMonthValue());
		entrie.setTweek(now.getDayOfWeek().getValue());

		entrie.setCreateBy(getLoginName());
		entrie.setCreateTime(LocalDateTime.now());
		return this.insertTimeEntries(entrie);
	}
}
